<?php
/**
 * Notes:
 * User: wudg <544038230@qq.com>
 * Date: 2025/9/15 11:41
 */

namespace App\Dao\Users;


use App\Constants\System\SysStatusConst;
use App\Dao\BaseDao;
use App\Model\Users\UsersAmountLog;

class UsersAmountLogsDao extends BaseDao
{

    /**
     * 获取指定月份的
     * @param int $uid
     * @param string $month
     * @return UsersAmountLog[]|\Hyperf\Collection\Collection|\Hyperf\Database\Model\Builder[]|\Hyperf\Database\Model\Collection|\Hyperf\Database\Query\Builder[]
     */
    public function groupByMonth(int $uid,string $month,int $accountId = null|0)
    {
        $startDate = date('Y-m-01', strtotime("$month-01"));
        $endDate = date('Y-m-t', strtotime("$month-01"));
        $model = UsersAmountLog::query()
        ->selectRaw("SUM(amount) AS total_amount,type");
        $model->where('uid','=',$uid);
        if(!empty($accountId))
        {
            $model->where('account_id','=',$accountId);
        }
        $model->where('status','=',SysStatusConst::NORMAL->value);
        return $model->whereBetween('save_date',[$startDate,$endDate])
            ->groupBy('type')->get();
    }



    public function groupByType(int $uid,string $start, string $end, int $accountId = null|0)
    {
        $model = UsersAmountLog::query()
            ->selectRaw("SUM(amount) AS total_amount,type");

        $model->where('uid','=',$uid)->where('status','=',SysStatusConst::NORMAL->value);
        if($accountId) $model->where('account_id','=',$accountId);

        return $model->whereBetween('save_date',[$start,$end])
            ->groupBy('type')->get();
    }


    /**
     * 按分类聚合收入信息
     * @param int $uid
     * @param string $start
     * @param string $end
     * @param int $type
     * @param int $accountId
     * @return UsersAmountLog[]|\Hyperf\Collection\Collection|\Hyperf\Database\Model\Builder[]|\Hyperf\Database\Model\Collection|\Hyperf\Database\Query\Builder[]
     */
    public function groupByCategory(int $uid,string $start, string $end, int $type = null|0, int $accountId = null|0)
    {
        $model = UsersAmountLog::query()
            ->selectRaw("SUM(amount) AS total_amount,cate_id");

        $model->where('uid','=',$uid)->where('status','=',SysStatusConst::NORMAL->value);
        if($accountId) $model->where('account_id','=',$accountId);
        if($type) $model->where('type','=',$type);
        return $model->whereBetween('save_date',[$start,$end])
            ->groupBy('cate_id')->get();
    }


    /**
     * 按账户聚合信息
     * @param int $uid
     * @param string $start
     * @param string $end
     * @param int $type
     * @return UsersAmountLog[]|\Hyperf\Collection\Collection|\Hyperf\Database\Model\Builder[]|\Hyperf\Database\Model\Collection|\Hyperf\Database\Query\Builder[]
     */
    public function groupByAccount(int $uid, string $start, string $end,int $type = null|0)
    {
        $model = UsersAmountLog::query()
            ->selectRaw("SUM(amount) AS total_amount,account_id");
        $model->where('uid','=',$uid)->where('status','=',SysStatusConst::NORMAL->value);
        if($type) $model->where('type','=',$type);
        return $model->whereBetween('save_date',[$start,$end])
            ->groupBy('account_id')->get();
    }

    public function getInfoByPk(int $pk, array $with = [])
    {
        $model = UsersAmountLog::query();
        if(!empty($with)) $model->with($with);
        $model->where('id','=',$pk)->where('status','=',SysStatusConst::NORMAL->value);
        return $model->first();

    }
}